import asyncio
import sys
from pathlib import Path
from fastapi import FastAPI, Header, WebSocket, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import dotenv
from pydantic import BaseModel


class SpiderConfig(BaseModel):
    url: str
    selectors: dict[str, list] = {}
    return_fields: list[str] = [
            "url",
            "title",
            "text_content",
            "markdown",
            "content",
            "published_time",
            "byline",
            "navigators",
    ]


dotenv.load_dotenv()
# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.append(str(project_root))

from wudi_crawler.crawler import Crawler
import os

SERVER_HOST = os.environ.get("SERVER_HOST", "localhost")
SERVER_PORT = os.environ.get("SERVER_PORT", "8801")

templates = Jinja2Templates(
    directory=(Path(__file__).parent / Path("templates")).resolve()
)


app = FastAPI()

app.mount(
    "/static",
    StaticFiles(directory=(Path(__file__).parent / Path("static")).resolve()),
    name="static",
)


@app.post("/api")
async def homepage(spider_config: SpiderConfig):
    # 将服务器配置传递给前端
    if not any(spider_config.selectors.values()):
        selectors = None
    else:
        # selectors = spider_config.selectors
        selectors = {k: v for k, v in spider_config.selectors.items() if v}
    async with Crawler(
        headless=False,
        router_config_path="./static/selectors.json",
        selectors=selectors,
    ) as crawler:
        item = await crawler.fetch(
            url=spider_config.url,
            return_fields=spider_config.return_fields,
        )

    # 确保返回所有需要的字段
    result = {
        "url": item.get("url", ""),
        "title": item.get("title", ""),
        "byline": item.get("byline", ""),
        "published_time": item.get("published_time", ""),
        "navigation": item.get("navigators", ""),
        "content": item.get("content", ""),
        "markdown": item.get("markdown", ""),
        "crawl_time": item.get("crawl_time", ""),
        "content_type": (
            "html"
            if item.get("content")
            else "markdown" if item.get("markdown") else "json"
        ),
    }

    return result


@app.get("/test", response_class=HTMLResponse)
async def test_page(request: Request):
    # 将服务器配置传递给前端
    context = {"server_host": SERVER_HOST, "server_port": SERVER_PORT}
    return templates.TemplateResponse(
        request=request, name="test.html", context=context
    )


@app.get("/", response_class=HTMLResponse)
async def homepage(request: Request):
    # 将服务器配置传递给前端
    context = {"server_host": SERVER_HOST, "server_port": SERVER_PORT}
    return templates.TemplateResponse(
        request=request, name="index.html", context=context
    )


if __name__ == "__main__":
    import uvicorn

    # 传递从 .env 读取的 host 和 port
    uvicorn.run(
        app=app,  # 应用路径（当前文件:app实例）
        host=SERVER_HOST,
        port=int(SERVER_PORT),
        # reload=DEBUG  # 若开启调试模式，可启用热重载（开发环境用）
    )
